#include "mission_mgr.h"
#include <server_share/lua_engine.h>
#include <server_share/server_def.h>
#include <nel/misc/path.h>
#include <game_share/string_spliter.h>
#include <string>

using namespace std;
using namespace ZS;
using namespace NLMISC;
using namespace DEF;

#define CHECK_ARGC( func, argc ) \
	int n = lua_gettop(L);  \
	if(n != (argc))	\
	{	\
		getGlobal_Loger().displayNL( "%s argument count should be %d, it is %d", #func, argc, n );	\
		lua_pop(L,n);	\
		return 0;		\
	}

static int Lua_LoadLua(lua_State* L)
{
	CHECK_ARGC(Lua_LoadLua,1)

	const char* szFileName = lua_tostring(L,-1);
	lua_pop(L,n);

	string script = NLMISC::CPath::lookup(string(szFileName));

	if ( script.size() > 0 )
	{
		LuaEngine.LoadLuaFile(script.c_str());

		CStringSpliter spliter("_.");
		spliter.splitString(string(szFileName));

		if ( spliter.getStr(0) == "mission" )
		{
			sint32 mission_id = spliter.getInt32(1);
			if ( mission_id > 0 )
			{
				bool reg = MissionMgr.register_mission(mission_id);
				if (!reg)
				{
					getGlobal_Loger().displayNL ("register mission repeat %d.", mission_id);
				}
			}
		}
	}

	return 0;
}

static int Lua_RegMissionEvent(lua_State* L)
{
	CHECK_ARGC(Lua_RegMissionEvent,1)

	EVENT_ID   event_id = lua_tonumber(L,-1);
	MISSION_ID mission_id = lua_tonumber(L,-1);
	lua_pop(L,n);

	MissionMgr.register_event( event_id, mission_id );
	return 0;
}





extern const luaL_Reg user_funcs[];

#ifndef LUA_FUNCTION_IMPLEMENTATION
#define LUA_FUNCTION_IMPLEMENTATION
#endif

#include "lua_function.h"
